;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.SalesPriceSearch", [])
        .controller("SalesPriceSearchCtrl", ["$scope", "ybWidget", "ybConfig", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", function ($scope, ybWidget, ybConfig, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService ) {
            $scope.markInfo = {
                curRow: -1,
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.tbConf = {
                scrollWidth: 1250,
                tableCol: [
                    {
                        name: "工厂名称",
                        dataField: "companyName",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDesc",
                        width: 250,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "价格",
                        dataField: "priceShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "方案名称",
                        dataField: "activityName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },                    
                    {
                        name: "库存",
                        dataField: "amount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "尺寸",
                        dataField: "materialsMeasure",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "重量",
                        dataField: "weight",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };

            $scope.condition = {};
            $scope.toolbarConfig = {};
            $scope.dataList = [];
            $scope.componyList = [];

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/marketingActivity/priceExport"),
                allUrl: ybHttp.RequestUrl("device", "/marketingActivity/salePriceExportAll"),
                sectionParam: {
                    reportVOS: []
                }
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index;
            };

            function getDataList(curPage) {
                if (!$scope.condition.orgId) {
                    ybWidget.ShowToaster('error', '请选择工厂');
                    return;
                }

                var service = "device";
                var reqUrl = "/marketingActivity/salePriceSearch";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var resT = resp.value || {};
                    var pageInfoT = resT.pageInfo || {};

                    angular.forEach(resT.pageInfoList, function (item) {
                        item.priceShow = $filter("decimalPlaces")(item.price, 2);
                        item.isChecked = false;
                    })
                    
                    $scope.markInfo = {
                        curRow: -1,
                        allChecked: false
                    };

                    $scope.dataList = resT.pageInfoList;
                    $scope.pageInfo.totalRows = pageInfoT.total;
                    $scope.pageInfo.totalPages = pageInfoT.pages;
                });
            }

            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            $scope.handleBlur = function (e) {
                $scope.condition.materialsNos = UserContextService.MultiInput(e.target.value);
            };

            $scope.selectAll = function () {
                for (var i = 0; i < $scope.dataList.length; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                getBillIds();
            };

            $scope.selectSingle = function (item) {
                var allSelected = false;
                if (!item.isChecked) {
                    allSelected = false;
                } else {
                    allSelected = checkAll($scope.dataList);
                }
                $scope.markInfo.allChecked = allSelected;
                getBillIds();
            };

            function checkAll(list) {
                var allSelected = true;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        allSelected = false;
                        break;
                    }
                }
                return allSelected;
            }

            function getBillIds() {
                $scope.exportParam.sectionParam.reportVOS = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.reportVOS.push(item);
                    }
                });
            }

            $scope.importData = function () {
                var param = {
                    name: "零件范围",
                    templateReqService: "device",
                    templateReqUrl: "/marketingActivity/downloadSaleDealerExcel",
                    importReqService: "device",
                    importReqUrl: "/marketingActivity/importSaleDealerExcel",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param);
            };

            var btnPermissionCode = {
                saleDealerImport: window.ActionCode.saleDealerImport,
            };

            $scope.btnPermission = {
                saleDealerImport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);